package com.changeover.gwt.client.view.impl;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;

import com.google.gwt.cell.client.CheckboxCell;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent.ListHandler;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.view.client.ListDataProvider;

public class CustomerMainDataTab extends Composite {

    public CustomerMainDataTab() {
        VerticalPanel customerAccountPanel = new VerticalPanel();
        customerAccountPanel.setSize("860px", "185px");

        VerticalPanel customerDataWrapper = new VerticalPanel();
        customerAccountPanel.add(customerDataWrapper);
        customerDataWrapper.setSize("870px", "");

        Label customerDataLabel_1 = new Label("Adresses");
        customerDataLabel_1.setStyleName("table-title2");
        customerDataWrapper.add(customerDataLabel_1);
        customerDataWrapper.add(initAdresses());

        Label commLabel = new Label("Communication");
        commLabel.setStyleName("table-title2");
        customerDataWrapper.add(commLabel);
        customerDataWrapper.add(initCommes());
        
        initWidget(customerAccountPanel);
    }
    
    public CellTable<Adress> initAdresses() {
        // Create a CellTable.
        CellTable<Adress> table = new CellTable<Adress>();

        // Create name column.
        TextColumn<Adress> idColumn = new TextColumn<Adress>() {
          @Override
          public String getValue(Adress Adress) {
            return Adress.id;
          }
        };

        // Make the name column sortable.
        idColumn.setSortable(true);

        // Create address column.
        TextColumn<Adress> typeColumn = new TextColumn<Adress>() {
          @Override
          public String getValue(Adress Adress) {
            return Adress.type;
          }
        };
        TextColumn<Adress> titleColumn = new TextColumn<Adress>() {
            @Override
            public String getValue(Adress Adress) {
                return Adress.title;
            }
        };
        TextColumn<Adress> title2Column = new TextColumn<Adress>() {
            @Override
            public String getValue(Adress Adress) {
                return Adress.title2;
            }
        };
        TextColumn<Adress> nameColumn = new TextColumn<Adress>() {
            @Override
            public String getValue(Adress Adress) {
                return Adress.name;
            }
        };
        TextColumn<Adress> name2Column = new TextColumn<Adress>() {
            @Override
            public String getValue(Adress Adress) {
                return Adress.name2;
            }
        };
        TextColumn<Adress> additionalColumn = new TextColumn<Adress>() {
            @Override
            public String getValue(Adress Adress) {
                return Adress.additional;
            }
        };
        TextColumn<Adress> streetColumn = new TextColumn<Adress>() {
            @Override
            public String getValue(Adress Adress) {
                return Adress.street;
            }
        };
        TextColumn<Adress> postalColumn = new TextColumn<Adress>() {
            @Override
            public String getValue(Adress Adress) {
                return Adress.postal;
            }
        };
        TextColumn<Adress> townColumn = new TextColumn<Adress>() {
            @Override
            public String getValue(Adress Adress) {
                return Adress.town;
            }
        };
        
        // Add the columns.
        table.addColumn(idColumn, "#");
        table.addColumn(typeColumn, "Type");
        table.addColumn(titleColumn, "Title-1");
        table.addColumn(title2Column, "Title2");
        table.addColumn(nameColumn, "Name-1");
        table.addColumn(name2Column, "Name-2");
        table.addColumn(additionalColumn, "Additional");
        table.addColumn(streetColumn, "Street");
        table.addColumn(postalColumn, "Postal");
        table.addColumn(townColumn, "Town");

        // Create a data provider.
        ListDataProvider<Adress> dataProvider = new ListDataProvider<Adress>();

        // Connect the table to the data provider.
        dataProvider.addDataDisplay(table);

        // Add the data to the data provider, which automatically pushes it to the
        // widget.
        List<Adress> list = dataProvider.getList();
        for (Adress Adress : ADRESSES) {
          list.add(Adress);
        }

        // Add a ColumnSortEvent.ListHandler to connect sorting to the
        // java.util.List.
        ListHandler<Adress> columnSortHandler = new ListHandler<Adress>(
            list);
        columnSortHandler.setComparator(idColumn,
            new Comparator<Adress>() {
              public int compare(Adress o1, Adress o2) {
                if (o1 == o2) {
                  return 0;
                }

                // Compare the name columns.
                if (o1 != null) {
                  return (o2 != null) ? o1.id.compareTo(o2.id) : 1;
                }
                return -1;
              }
            });
        table.addColumnSortHandler(columnSortHandler);

        // We know that the data is sorted alphabetically by default.
        table.getColumnSortList().push(idColumn);
        table.setStyleName("table-panel2");

        return table;
    }
    
    
    public CellTable<Comm> initCommes() {
        // Create a CellTable.
        CellTable<Comm> table = new CellTable<Comm>();

        // Create name column.
        TextColumn<Comm> typeColumn = new TextColumn<Comm>() {
          @Override
          public String getValue(Comm Comm) {
            return Comm.type;
          }
        };

        typeColumn.setSortable(true);

        CheckboxCell prCell = new CheckboxCell();
        Column<Comm, Boolean> prColumn = new Column<Comm, Boolean>(prCell) {
            @Override
            public Boolean getValue(Comm object) {
                return object.preffered;
            }
        };
        TextColumn<Comm> dataColumn = new TextColumn<Comm>() {
            @Override
            public String getValue(Comm Comm) {
                return Comm.data;
            }
        };
        TextColumn<Comm> contactColumn = new TextColumn<Comm>() {
            @Override
            public String getValue(Comm Comm) {
                return Comm.contact;
            }
        };
        
        // Add the columns.
        table.addColumn(typeColumn, "Type");
        table.addColumn(prColumn, "Preferred");
        table.addColumn(dataColumn, "Data");
        table.addColumn(contactColumn, "Contact");

        // Create a data provider.
        ListDataProvider<Comm> dataProvider = new ListDataProvider<Comm>();

        // Connect the table to the data provider.
        dataProvider.addDataDisplay(table);

        // Add the data to the data provider, which automatically pushes it to the
        // widget.
        List<Comm> list = dataProvider.getList();
        for (Comm Comm : COMMES) {
          list.add(Comm);
        }

        // Add a ColumnSortEvent.ListHandler to connect sorting to the
        // java.util.List.
        ListHandler<Comm> columnSortHandler = new ListHandler<Comm>(
            list);
        columnSortHandler.setComparator(typeColumn,
            new Comparator<Comm>() {
              public int compare(Comm o1, Comm o2) {
                if (o1 == o2) {
                  return 0;
                }

                // Compare the name columns.
                if (o1 != null) {
                  return (o2 != null) ? o1.type.compareTo(o2.type) : 1;
                }
                return -1;
              }
            });
        table.addColumnSortHandler(columnSortHandler);

        // We know that the data is sorted alphabetically by default.
        table.getColumnSortList().push(typeColumn);
        table.setStyleName("table-panel2");

        return table;
    } 
    
    private static List<Adress> ADRESSES = Arrays.asList(
        new Adress("01", "1", "Company", "", "Sample Company", "Sam samle", "c/o Company AG", "Sampleway1", "123456", "Sample town"),
        new Adress("04", "1", "Company", "", "Sample", "Company", "", "Sampleway2", "123456", "Sample town"),
        new Adress("91", "2", "Mr.", "", "Sam", "Sample", "", "Sampleway3", "123456", "Sample town"));
    
    private static class Adress {
        private final String id;
        private final String type;
        private final String title;
        private final String title2;
        private final String name;
        private final String name2;
        private final String additional;
        private final String street;
        private final String postal;
        private final String town;

        public Adress(String id, String type, String title, String title2, String name,
            String name2, String additional, String street, String postal, String town) {
            super();
            this.id = id;
            this.type = type;
            this.title = title;
            this.title2 = title2;
            this.name = name;
            this.name2 = name2;
            this.additional = additional;
            this.street = street;
            this.postal = postal;
            this.town = town;
        }
    }
    
    private static List<Comm> COMMES = Arrays.asList(
        new Comm("Phone Business", true, "040-123456-11", "Mrs. Sample"),
        new Comm("Phone Private", false, "040-321654", ""),
        new Comm("Phone Mobile", false, "0111-123456", ""),
        new Comm("eMail Business", false, "office@sample.de", ""),
        new Comm("eMail Private", false, "sample@mgx.de", ""));
    
    private static class Comm {
        private final String type;
        private final boolean preffered;
        private final String data;
        private final String contact;
        
        public Comm(String type, boolean preffered, String data, String contact) {
            this.type = type;
            this.preffered = preffered;
            this.data = data;
            this.contact = contact;
        }
    }
    
}
